package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Collect;
import cn.wjh.business.domain.Works;
import cn.wjh.business.mapper.CollectMapper;
import cn.wjh.business.service.ICollectService;
import cn.wjh.business.service.ICompetitionService;
import cn.wjh.business.service.IWorksService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 收藏Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-21
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {
	@Resource
	private CollectMapper collectMapper;

	@Resource
	private ICompetitionService competitionService;

	@Resource
	private IWorksService worksService;

	/**
	 * 查询收藏
	 *
	 * @param collectId 收藏主键
	 * @return 收藏
	 */
	@Override
	public Collect selectCollectByCollectId(Long collectId) {
		return collectMapper.selectCollectByCollectId(collectId);
	}

	/**
	 * 查询收藏列表
	 *
	 * @param collect 收藏
	 * @return 收藏
	 */
	@Override
	public List<Collect> selectCollectList(Collect collect) {
		List<Collect> list = collectMapper.selectCollectList(collect);
		/*获取收藏的比赛*/
		if (collect.getType().equals("competition")){
			list.forEach(item->{
				item.setData(competitionService.selectCompetitionByCompetitionId(item.getDataId()));
			});
		}else{
			/*收藏的作品*/
			list.forEach(item->{
				item.setData(worksService.selectWorksByWorksId(item.getDataId()));
			});
		}
		return list;
	}

	/**
	 * 新增收藏
	 *
	 * @param collect 收藏
	 * @return 结果
	 */
	@Override
	public int insertCollect(Collect collect) {
		collect.setCreateTime(DateUtils.getNowDate());
		collect.setCreateBy(SecurityUtils.getUsername());
		collect.setUserId(SecurityUtils.getUserId());
		try {
			/*收藏*/
			return collectMapper.insertCollect(collect);
		} catch (DuplicateKeyException duplicateKeyException) {
			/*取消收藏*/
			LambdaQueryWrapper<Collect> deleteWrapper = new QueryWrapper<Collect>().lambda()
					.eq(Collect::getType, collect.getType())
					.eq(Collect::getUserId, SecurityUtils.getUserId())
					.eq(Collect::getDataId, collect.getDataId());
			return collectMapper.delete(deleteWrapper);
		}
	}

	/**
	 * 修改收藏
	 *
	 * @param collect 收藏
	 * @return 结果
	 */
	@Override
	public int updateCollect(Collect collect) {
		return collectMapper.updateCollect(collect);
	}

	/**
	 * 批量删除收藏
	 *
	 * @param collectIds 需要删除的收藏主键
	 * @return 结果
	 */
	@Override
	public int deleteCollectByCollectIds(Long[] collectIds) {
		return collectMapper.deleteCollectByCollectIds(collectIds);
	}

	/**
	 * 删除收藏信息
	 *
	 * @param collectId 收藏主键
	 * @return 结果
	 */
	@Override
	public int deleteCollectByCollectId(Long collectId) {
		return collectMapper.deleteCollectByCollectId(collectId);
	}

	@Override
	public boolean isCollect(Long dataId, String type) {
		LambdaQueryWrapper<Collect> selectWrapper = new QueryWrapper<Collect>().lambda()
				.select(Collect::getCollectId)
				.eq(Collect::getType, type)
				.eq(Collect::getUserId, SecurityUtils.getUserId())
				.eq(Collect::getDataId, dataId);
		Collect collect = this.getOne(selectWrapper);
		return StringUtils.isNotNull(collect);
	}

	/**
	 * 获取收藏数量
	 *
	 * @param dataId 业务Id
	 * @param type   业务类型
	 * @return 数量
	 */
	@Override
	public Long countByDataIdAndType(Long dataId, String type) {
		LambdaQueryWrapper<Collect> selectWrapper = new QueryWrapper<Collect>().lambda()
				.select(Collect::getCollectId)
				.eq(Collect::getType, type)
				.eq(Collect::getDataId, dataId);
		return this.count(selectWrapper);
	}

	/**
	 * 获取当前用户所有作品被收藏的数量
	 */
	@Override
	public Long countByCurrentUser(SysUser user) {
		Works works = new Works();
		works.setUserId(user.getUserId());
		List<Works> worksList = worksService.myWorks(works);/*作品列表*/
		if (worksList.isEmpty()) return 0L;
		AtomicLong total = new AtomicLong(0);/*总收藏数量*/
		worksList.forEach(w -> {
			total.addAndGet(w.getCollect());
		});
		return total.get();
	}
}
